$(function () {
    Table.builder()
        .listUrl(BAIT_MANAGEMENT_CONTENT_LIST)
        .onLoadSuccess(mergeCells)
        .init();

    $("#tableAjaxId").on("click", ".updatePrice", function () {
        var id = $(this).attr("dataId")
        Form.clearValues($("#updatePriceForm"));
        Request.builder()
            .url(BAIT_MANAGEMENT_DETAIL + "/" + id)
            .isToken(true)
            .callback(function (e) {
                Form.setValues($("#updatePriceForm"), e.data);
            }).get();
    })

    $("#baitSizeSub").click(function () {
        var data = Form.getValues($("#updatePriceForm"));
        Request.builder()
            .url(BAIT_MANAGEMENT_UPDATE_PRICE + "/" + data.baitSizeId)
            .data(data)
            .isToken(true)
            .callback(function (e) {
                if (e.code === "00000") {
                    LayerUtil.successMsg(ERROR_MSG.SUCCESS)
                    setTimeout(function () {
                        $("#tableAjaxId").bootstrapTable('refresh', {pageNumber: 1});
                        $("#updatePriceDiv").find(".close").trigger("click");
                    }, 1000)
                } else {
                    LayerUtil.failMsg(e.msg != null ? e.msg : ERROR_MSG.FAIL)
                }
            }).post();
    })

});

columns = [
    {
        field: 'baitCompanyName',
        title: '供应商',
        align: 'center',
        valign: 'middle'
    }, {
        field: 'baitBrandName',
        title: '品牌',
        align: 'center',
        valign: 'middle'
    }, {
        field: 'name',
        title: '规格(mm)',
        align: 'center',
        valign: 'middle',
        formatter: function (value, row, index) {
            return value + "mm";
        }
    }, {
        field: 'price',
        title: '价格(元)',
        align: 'center',
        valign: 'middle'
    }, {
        field: 'useStatus',
        title: '使用状态',
        align: 'center',
        valign: 'middle',
        formatter: function (value, row, index) {
            if (value === 0) {
                return "<span style='color:#999999;font-weight: bold;'>未使用</span>";
            } else if (value === 1) {
                return "<span style='color:#73CDAA;font-weight: bold;'>已使用</span>";
            }
        }
    }, {
        field: 'lastModifyTime',
        title: '最后修改时间',
        align: 'center',
        valign: 'middle',
        formatter: function (value, row, index) {
            return DateUtil.changeDateToString(value, DateFormatEnum.YYYY_MM_DD);
        }
    }, {
        title: "操作",
        align: 'center',
        valign: 'middle',
        width: 160,
        formatter: baitBrandOption
    }
]

function baitBrandOption(value, row, index) {
    var html = '';
    html += '<div class="d-flex justify-content-center">' +
        '<a class="btn btn-xs btn-default updatePrice" data-toggle="modal" data-target="#updatePriceDiv" dataId="' + row.id + '" title="修改价格" >修改价格</a>' +
        '</div>'
    return html;
}

function mergeCells() {
    var table = document.getElementById("tableAjaxId");
    var rows = table.rows;
    var currentCompany = null;
    var currentCompany1 = null;
    var rowspan = 1;
    var rowspan1 = 1;

    for (var i = 1; i < rows.length; i++) {
        var company = rows[i].cells[0].innerText;
        if (company !== "没有找到匹配的记录") {
            var company1 = rows[i].cells[1].innerText;
            if (company1 == currentCompany1 && company == currentCompany) {
                rowspan1++;
                rows[i].deleteCell(1);
                if (rows[i - 1].cells[0].innerText == currentCompany) {
                    rows[i - rowspan1 + 1].cells[1].rowSpan = rowspan1;
                } else {
                    rows[i - rowspan1 + 1].cells[0].rowSpan = rowspan1;
                }
            } else {
                currentCompany1 = company1;
                rowspan1 = 1;
            }
            if (company == currentCompany) {
                rowspan++;
                rows[i].deleteCell(0);
                rows[i - rowspan + 1].cells[0].rowSpan = rowspan;
            } else {
                currentCompany = company;
                rowspan = 1;
            }
        }


    }
}
